package cn.ronghuanet.base.util;

import cn.ronghuanet.base.exception.ResponseCode;
import lombok.Data;
import org.springframework.expression.spel.ast.NullLiteral;

@Data
public class AjaxResult {

    private Integer code;   // 响应码
    private Boolean success;
    private String msg;
    private Object objData;

    public static AjaxResult success(){
        return  success(null);
    }

    public static AjaxResult successMsg(String msg){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMsg(msg);
        ajaxResult.setSuccess(true);
        ajaxResult.setObjData(null);
        ajaxResult.setCode(ResponseCode.RESPONSE_CODE_200.getCode());    // 当后端请求成功时,默认返回前端是200
        return ajaxResult;
    }

    public static AjaxResult success(Object data){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMsg(ResponseCode.RESPONSE_CODE_200.getMessage());
        ajaxResult.setSuccess(true);
        ajaxResult.setObjData(data);
        ajaxResult.setCode(ResponseCode.RESPONSE_CODE_200.getCode());    // 当后端请求成功时,默认返回前端是200
        return ajaxResult;
    }


    public static AjaxResult error(){
        return error(null);
    }
    public static AjaxResult error(String msg){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMsg(msg);
        ajaxResult.setSuccess(false);
        return ajaxResult;
    }

    // 返回前端错误信息时,不仅需要错误信息,还需要这个错误信息所对应的响应码
    public static AjaxResult error(Integer code,String msg){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMsg(msg);
        ajaxResult.setSuccess(false);
        ajaxResult.setCode(code);
        return ajaxResult;
    }
}
